﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using FAP.GUI;
using FAP.DataAccess;
using FAP.DataAccess.BLL;
using System.Diagnostics;

namespace FAP_GUI.UC
{
    public partial class ucLoadDanhSach : UserControl
    {
        //FAP.DataAccess.FAPDataContext context = new FAP.DataAccess.FAPDataContext();
        public ucLoadDanhSach()
        {
            InitializeComponent();
        } 
        // bị lỗi checkbox all
        bool flag_chb = false;// cho load lại listview
        bool flag_chb1 = false; // cho checkbox all
        bool flag_loadSearch = false;// khi search button khoi chay loadlistview item.checked
        int demItem = 0;
        List<website> lvdisplay = new List<website>();
        List<website> select_webs = new List<website>();
        

        List<website> lws = new List<website>();

        private void LoadDanhSach(List<website> sw)
        {
            lvdisplay = new List<website>();
            website ws;
            for (int i = 0; i < sw.Count; i++)
            {
                ws = new website();
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems.Add(sw[i].id.ToString());
                lvi.SubItems.Add(sw[i].link.ToString());
                ws.id = sw[i].id;
                ws.link = sw[i].link;
                ws.tag = sw[i].tag;
                lvdisplay.Add(ws);

                lvLoadDanhSach.Items.Add(lvi);
                if (select_webs == null)
                    continue;
                 for (int j = 0; j < select_webs.Count; j++)
                {
                    if (sw[i].id == select_webs[j].id)
                    {
                        lvLoadDanhSach.Items[i].Checked = true;
                        break;
                    }
                }
            }
            demItem = 0;
            for (int i = 0; i < lvdisplay.Count; i++)
            {
                if (lvLoadDanhSach.Items[i].Checked == true)
                {
                    demItem++;
                }
            }
        }
        private void ucLoadDanhSach_Load(object sender, EventArgs e)

        {
            try
            {
                List<website> sw = website.websiteSelectAll().Where(w => w.trangthai == 1).ToList();
                lws = sw;
                LoadDanhSach(sw);
                demItem = 0;
            }
            catch { };
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<website> sw = website.websiteSelectAll().Where(w => w.trangthai == 1).ToList();
            lws = sw;
            LoadLVDanhSachWebsite();
            //flag_chb1 = true;
            //flag_loadSearch = true;
            //string search = BUS.BUS_GUI.TiengVietKhongDau(txtSearch.Text);
            //List<website> ws = context.websites.Where(w => w.trangthai == 1 && (w.tag.Contains(search) || w.link.Contains(search))).ToList();
            //lvLoadDanhSach.Items.Clear();
            //LoadDanhSach(ws);
            //flag_loadSearch = false;
            //flag_chb1 = false;
        }

        private void ckbCheckAll_CheckedChanged(object sender, EventArgs e)
        {   
            if (flag_chb1 == true)
                return;
            flag_chb = true;
            //string search = txtSearch.Text;
            DisplaySelection();
            flag_chb = false;
        }

        private void lvLoadDanhSach_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            website ws = new website();
            
            if (flag_chb == true)
                return;
            if (flag_loadSearch == true)
                return;
            int j = lvLoadDanhSach.Items.Count;
            if (e.Item.Checked == false)
            {
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = false;
                    flag_chb1 = false;
                }
                demItem--;
                e.Item.BackColor = System.Drawing.Color.White;

                for (int i = 0; i < select_webs.Count; i++)
                {
                    if (e.Item.SubItems[1].Text.ToString() == select_webs[i].id.ToString())
                    {
                        
                        select_webs.RemoveAt(i);
                        break;
                    }
                }
                
            }
            else
            {
                demItem++;
                e.Item.BackColor = System.Drawing.Color.Lavender;
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = true;
                    flag_chb1 = false;
                }
                bool flag_selectEdit = false;
                if (select_webs != null)
                {
                    for (int i = 0; i < select_webs.Count; i++)
                    {
                        if (e.Item.SubItems[1].Text.ToString() == select_webs[i].id.ToString())
                        {
                            flag_selectEdit = true;
                        }
                    }
                }
                if (flag_selectEdit == false)
                {
                    ws.id = int.Parse(e.Item.SubItems[1].Text.ToString());
                    ws.link = e.Item.SubItems[2].Text.ToString();
                    if (select_webs == null)
                        select_webs = new List<website>();
                    select_webs.Add(ws);
                }
                
            }
            //int j = lvLoadDanhSach.Items.Count - 1;
            //for (int i = 0; i<= j/2; i++)
            //{
            //    if (lvLoadDanhSach.Items[i].Checked == false || lvLoadDanhSach.Items[j-i].Checked == false)
            //    {
            //        flag_chb1 = true;
            //        chbCheckAll.Checked = false;
            //        flag_chb1 = false;
            //        return;
            //    }
            //}
            //chbCheckAll.Checked = true;

        }

        private void lvLoadDanhSach_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender, e);
            }
        }
        private void DisplaySelection()
        {
            if (chbCheckAll.Checked == true)
            {
                
                for (int i = lvLoadDanhSach.Items.Count - 1; i >= 0; i--)
                {
                    lvLoadDanhSach.Items[i].Checked = true;
                    lvLoadDanhSach.Items[i].BackColor = System.Drawing.Color.Lavender;

                }
                demItem = lvLoadDanhSach.Items.Count;
                int lvdisplayCount = lvdisplay.Count;
                List<website> lwsTemp = new List<website>(lvdisplay);

                for (int i = 0; i < lvdisplayCount; i++)
                {
                    for (int j = 0; j < select_webs.Count; j++)
                    {
                        if (lwsTemp[i].id == select_webs[j].id)
                        {
                            lwsTemp.RemoveAt(i);
                            i--;
                            lvdisplayCount--;
                            break;
                        }
                    }
                    
                }
                select_webs.AddRange(lwsTemp);
            }
            else
            {
                List<website> temp = new List<website>();


                for (int i = lvLoadDanhSach.Items.Count - 1; i >= 0; i--)
                {
                    lvLoadDanhSach.Items[i].Checked = false;
                    lvLoadDanhSach.Items[i].BackColor = System.Drawing.Color.White;

                }
                demItem = 0;
                for (int i = 0; i < lvdisplay.Count; i++)
                {
                    for (int j = 0; j < select_webs.Count; j++)
                    {
                        if (lvdisplay[i].id == select_webs[j].id)
                        {
                            select_webs.RemoveAt(j);
                            break;
                        }
                    }
                }
            }
        }
        public delegate void SelectedHandler(List<website> sender);
        public event SelectedHandler onSelectedWebSite;

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (onSelectedWebSite != null)
            {
                onSelectedWebSite(select_webs);
            }
        }

        private void LoadLVDanhSachWebsite()
        {
            //chbCheckAll.Checked = false;
            lvdisplay = new List<website>();
            website ws;
            lvLoadDanhSach.Items.Clear();
            string search = txtSearch.Text;
            int dem = -1;

            for (int i = 0; i < lws.Count; i++)
            {
                if (lws[i].tag != null && (lws[i].tag.Contains(search) || lws[i].link.Contains(search)))
                {
                    ws = new website();
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Add(lws[i].id.ToString());
                    lvi.SubItems.Add(lws[i].link.ToString());
                    ws.id = lws[i].id;
                    ws.link = lws[i].link.ToString();
                    ws.tag = lws[i].tag.ToString();
                    lvdisplay.Add(ws);

                    flag_chb = true;
                    lvLoadDanhSach.Items.Add(lvi);
                    dem++;
                    flag_chb = false;

                    for (int j = 0; j < select_webs.Count; j++)
                    {
                        if (lws[i].id == select_webs[j].id)
                        {
                            lvLoadDanhSach.Items[dem].Checked = true;
                            break;
                        }
                    }
                }
            }
            demItem = 0;
            for (int i = 0; i < lvdisplay.Count; i++)
            {
                if (lvLoadDanhSach.Items[i].Checked == true)
                {
                    demItem++;
                }
            }

            int count = lvLoadDanhSach.Items.Count;
            if (demItem == count)
            {
                flag_chb1 = true;
                chbCheckAll.Checked = true;
                flag_chb1 = false;
            }
            else
            {
                flag_chb1 = true;
                chbCheckAll.Checked = false;
                flag_chb1 = false;
            }
        }

    }

   

}